//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "VolatilityInterpolationSpecifier.h"
using namespace Cephei::QL::Models::Marketmodels::Models;
#include <gen/QL/Models/Marketmodels/Models/PiecewiseConstantVariance.h>
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::CVolatilityInterpolationSpecifier (boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifier>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phVolatilityInterpolationSpecifier = NULL;
#endif
	_ppVolatilityInterpolationSpecifier = &childNative;
    
}
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::CVolatilityInterpolationSpecifier (QuantLib::VolatilityInterpolationSpecifier& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phVolatilityInterpolationSpecifier = NULL;
#endif
	_ppVolatilityInterpolationSpecifier = new boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifier> (&childNative);
    
    _VolatilityInterpolationSpecifierOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::CVolatilityInterpolationSpecifier (CVolatilityInterpolationSpecifier^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phVolatilityInterpolationSpecifier = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppVolatilityInterpolationSpecifier = new boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifier> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::CVolatilityInterpolationSpecifier (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phVolatilityInterpolationSpecifier = NULL;
#endif
	if (!t->IsSubclassOf(CVolatilityInterpolationSpecifier::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::CVolatilityInterpolationSpecifier (QuantLib::Handle<QuantLib::VolatilityInterpolationSpecifier>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phVolatilityInterpolationSpecifier = &childNative;
	_ppVolatilityInterpolationSpecifier = &static_cast<boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifier>>(childNative.currentLink());
    
    _VolatilityInterpolationSpecifierOwner = owner;
}
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::CVolatilityInterpolationSpecifier (QuantLib::Handle<QuantLib::VolatilityInterpolationSpecifier> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phVolatilityInterpolationSpecifier = &childNative;
	_ppVolatilityInterpolationSpecifier = &static_cast<boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifier>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::CVolatilityInterpolationSpecifier (QuantLib::VolatilityInterpolationSpecifier childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phVolatilityInterpolationSpecifier = NULL;
#endif
	_ppVolatilityInterpolationSpecifier = new boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifier> (new QuantLib::VolatilityInterpolationSpecifier (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::~CVolatilityInterpolationSpecifier ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppVolatilityInterpolationSpecifier != NULL)
    {
	    delete _ppVolatilityInterpolationSpecifier;
        _ppVolatilityInterpolationSpecifier = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::!CVolatilityInterpolationSpecifier ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppVolatilityInterpolationSpecifier != NULL)
    {
	    delete _ppVolatilityInterpolationSpecifier;
    }
}
QuantLib::VolatilityInterpolationSpecifier& Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::GetReference ()
{
    if (_ppVolatilityInterpolationSpecifier == NULL) throw gcnew NativeNullException ();
	return **_ppVolatilityInterpolationSpecifier;
}
boost::shared_ptr<QuantLib::VolatilityInterpolationSpecifier>& Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::GetShared ()
{
    if (_ppVolatilityInterpolationSpecifier == NULL) throw gcnew NativeNullException ();
	return *_ppVolatilityInterpolationSpecifier;
}
QuantLib::VolatilityInterpolationSpecifier* Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::GetPointer ()
{
    if (_ppVolatilityInterpolationSpecifier == NULL) throw gcnew NativeNullException ();
	return &**_ppVolatilityInterpolationSpecifier;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::VolatilityInterpolationSpecifier>& Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::GetHandle ()
{
	if (_phVolatilityInterpolationSpecifier == NULL)
	{
		_phVolatilityInterpolationSpecifier = new Handle<QuantLib::VolatilityInterpolationSpecifier> (*_ppVolatilityInterpolationSpecifier);
	}
	return *_phVolatilityInterpolationSpecifier;
}
#endif
bool Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::HasNative () 
{
	return (_ppVolatilityInterpolationSpecifier != NULL);
}

UInt64 Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::GetNoBigRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppVolatilityInterpolationSpecifier)->getNoBigRates ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::GetNoSmallRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppVolatilityInterpolationSpecifier)->getNoSmallRates ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::GetOffset::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppVolatilityInterpolationSpecifier)->getOffset ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::GetPeriod::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppVolatilityInterpolationSpecifier)->getPeriod ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::InterpolatedVariances::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::PiecewiseConstantVariance> >& _rv = (std::vector<boost::shared_ptr<QuantLib::PiecewiseConstantVariance> >&)(*_ppVolatilityInterpolationSpecifier)->interpolatedVariances ( );   
        CoVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ _nrv = gcnew CoVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>(gcnew CPiecewiseConstantVarianceVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::OriginalVariances::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::PiecewiseConstantVariance> >& _rv = (std::vector<boost::shared_ptr<QuantLib::PiecewiseConstantVariance> >&)(*_ppVolatilityInterpolationSpecifier)->originalVariances ( );   
        CoVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ _nrv = gcnew CoVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>(gcnew CPiecewiseConstantVarianceVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Models::IVolatilityInterpolationSpecifier^ Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::SetLastCapletVol (Double vol)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _vol = (QuantLib::Real)ValueHelper::Convert (vol);
    	(*_ppVolatilityInterpolationSpecifier)->setLastCapletVol ( _vol );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Models::IVolatilityInterpolationSpecifier^ Cephei::QL::Models::Marketmodels::Models::CVolatilityInterpolationSpecifier::SetScalingFactors (Cephei::IVector<Double>^ scales)
{
    CoVector<Double>^ _Cscales;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cscales = safe_cast<CoVector<Double>^> (scales);
        _Cscales->Lock();
        INativeVector<Double>^ _NCIscales = _Cscales->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCscales = safe_cast<CDoubleVector^>(_NCIscales);
        std::vector<QuantLib::Real>& _scales = static_cast<std::vector<QuantLib::Real>&> (_NCscales->GetReference ());
    	(*_ppVolatilityInterpolationSpecifier)->setScalingFactors ( _scales );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cscales != nullptr) _Cscales->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

